package pqt;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.JCheckBox;

public class ModificarPersonal extends JDialog {

	//private final JPanel contentPanel = new JPanel();
	private JTextField nombre;
	private JTextField apellido;
	private JTextField dni;
	private JPasswordField password;
	private BaseDeDatos db = BaseDeDatos.getInstance();
	private Personal personal[];
	final JComboBox listaPersonal = new JComboBox();
	final JCheckBox activo = new JCheckBox("");
	
	public ModificarPersonal(JPanel contentPanel, final ClosableTabbedPane tabbedPane) {
		//setBounds(100, 100, 773, 495);
		//getContentPane().setLayout(new BorderLayout());
		//contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		//getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new LineBorder(new Color(51, 153, 255)), "Modificar Personal", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds((contentPanel.getWidth()/2)-365, 100, 737, 373);
		contentPanel.add(panel);
		panel.setLayout(null);
		
		nombre = new JTextField();
		nombre.setBounds(326, 55, 212, 20);
		panel.add(nombre);
		nombre.setColumns(10);
		
		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNombre.setBounds(246, 53, 70, 20);
		panel.add(lblNombre);
		
		JLabel lblApellido = new JLabel("Apellido:");
		lblApellido.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblApellido.setBounds(246, 101, 70, 20);
		panel.add(lblApellido);
		
		apellido = new JTextField();
		apellido.setColumns(10);
		apellido.setBounds(326, 103, 212, 20);
		panel.add(apellido);
		
		JLabel lblDni = new JLabel("DNI:");
		lblDni.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDni.setBounds(246, 153, 70, 20);
		
		panel.add(lblDni);
		
		dni = new JTextField();
		dni.setColumns(10);
		dni.setBounds(326, 155, 212, 20);
		dni.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char caracter = e.getKeyChar();
				if(((caracter < '0') ||  (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE)) {
						e.consume();
				}
			}
		});
		panel.add(dni);
		
		JLabel lblClave = new JLabel("Clave:");
		lblClave.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblClave.setBounds(246, 204, 70, 20);
		panel.add(lblClave);
		
		password = new JPasswordField();
		password.setBounds(326, 204, 212, 20);
		
		panel.add(password);
		
		JLabel lblPrivilegio = new JLabel("Privilegio:");
		lblPrivilegio.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPrivilegio.setBounds(246, 245, 70, 20);
		panel.add(lblPrivilegio);
		
		final JComboBox privilegio = new JComboBox();
		privilegio.setBounds(326, 247, 212, 20);
		privilegio.addItem("Administrador");
		privilegio.addItem("Operario");
		panel.add(privilegio);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 tabbedPane.removeTab("Agregar Personal");
			}
		});
		btnCancelar.setBounds((panel.getX()+648), 480, 89, 23);
		contentPanel.add(btnCancelar);
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String pass = new String(password.getPassword());
				String permisos;
				Personal persona = new Personal();
				
				System.out.println(pass);
				
				
				
				if(privilegio.getSelectedItem().toString().equalsIgnoreCase("administrador"))
						permisos="1";
				else
						permisos="0";
				
				boolean existeDni = existeDni(personal[listaPersonal.getSelectedIndex()].getDocumento(),dni.getText());
				
				if(existeDni){
					JOptionPane.showMessageDialog(null,"El Dni ingresado ya corresponde a otra persona", "Mensaje de Error",JOptionPane.ERROR_MESSAGE);
				}
				else
				if(nombre.getText().equals("") || apellido.getText().equals("") || dni.equals("") || pass.equals("")) {

					JOptionPane.showMessageDialog(null,"Debe completar todos los campos", "Mensaje de Error",JOptionPane.ERROR_MESSAGE);
				}
				else {
					
					try {
						persona.setApellido(apellido.getText());
						persona.setNombre(nombre.getText());
						persona.setDocumento(dni.getText());
						persona.setPrivilegio(permisos);
						persona.setPassword(pass);
						persona.setActivo(activo.isSelected());
						
						if(!db.modificarUsuario(persona,personal[listaPersonal.getSelectedIndex()].getDocumento()))
						{
							
						}
						else
						{
							JOptionPane.showMessageDialog(null,"El usuario se modifico correctamente");
							consultarPersonal();
						}
					} catch (HeadlessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
								
			}

			
		});
		btnAceptar.setBounds(btnCancelar.getX()-105, 480, 89, 23);
		contentPanel.add(btnAceptar);
		
		
		listaPersonal.setBounds(10, 22, 200, 20);
		panel.add(listaPersonal);
		
		
		activo.setBounds(326, 295, 97, 23);
		panel.add(activo);
		
		JLabel lblActivo = new JLabel("Activo:");
		lblActivo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblActivo.setBounds(246, 295, 70, 20);
		panel.add(lblActivo);
		consultarPersonal();
		
		listaPersonal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(listaPersonal.getItemCount()>0){
					apellido.setText(personal[listaPersonal.getSelectedIndex()].getApellido());
					nombre.setText(personal[listaPersonal.getSelectedIndex()].getNombre());
					dni.setText(personal[listaPersonal.getSelectedIndex()].getDocumento());
					if(personal[listaPersonal.getSelectedIndex()].getPrivilegio().equalsIgnoreCase("Administrador"))
						privilegio.setSelectedIndex(0);
					else
						privilegio.setSelectedIndex(1);
					
					activo.setSelected(personal[listaPersonal.getSelectedIndex()].isActivo());
				}
				
			}
		});
	}
	
	
private boolean existeDni(String dniViejo, String dniNuevo) {
		
		for(int i=0;i<personal.length;i++){
						
			if(personal[i].getDocumento().equalsIgnoreCase(dniNuevo) && !personal[i].getDocumento().equalsIgnoreCase(dniViejo)){
				System.out.println("existe");
				return true;
			}
		}
		
		return false;
	}

	private void consultarPersonal() {
		
		listaPersonal.removeAllItems();
		try {
				
			personal = db.consultarPersonal();
				
			for(int i=0;i<personal.length;i++) {
				listaPersonal.addItem(personal[i].getApellido() + " " + personal[i].getNombre() );
			}
			
						
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
